/**
 * Copyright 2025 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iostream>

#include "custom_kernel_input_info.h"

using mindspore::kernel::op_plugin::KernelInputInfo;

extern "C" {

// Mock implementation of the cumsum_ext operator.
// Not fully implemented, only for certain test cases.
int CumsumExt(int nparam, void **params, int *ndims, int64_t **shapes, const char **dtypes, void *stream, void *extra) {
  std::cout << "op_plugin mock: CumsumExt called" << std::endl;
  constexpr int expected_nparam = 4;
  if (nparam != expected_nparam || params == nullptr || ndims == nullptr || shapes == nullptr) {
    std::cout << "Invalid parameters for cumsum_ext operator" << std::endl;
    return -1;
  }

  const int64_t *x = static_cast<const int64_t *>(params[0]);
  int64_t *out = static_cast<int64_t *>(params[nparam - 1]);

  auto kernel_input_info = static_cast<KernelInputInfo *>(extra);
  if (kernel_input_info == nullptr) {
    std::cout << "Invalid kernel input info for cumsum_ext operator" << std::endl;
    return -1;
  }

  auto dim = kernel_input_info->GetIntInput(1);
  if (dim != 0) {
    std::cout << "Expected dim = 0, but got " << dim << std::endl;
    return -1;
  }

  int x_rank = ndims[0];
  if (x_rank != 1) {
    std::cout << "Expected x_rank = 1, but got " << x_rank << std::endl;
    return -1;
  }
  int x_dim = shapes[0][0];
  for (int i = 0; i < x_dim; ++i) {
    out[i] = x[i];
    if (i > 0) {
      out[i] += out[i - 1];
    }
  }

  return 0;
}
}  // extern "C"
